package com.his.dictitem.service.Imp;

import com.his.dictitem.dao.DictItemChargeMapper;
import com.his.dictitem.entity.DictItemCharge;
import com.his.dictitem.service.DictItemChargeService;
import com.his.dictitem.vo.DictItemChargeVo;
import com.his.login.entity.DictEmployee;
import com.xiao.common.util.BeanUtil;
import com.xiao.common.util.WorkIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class DictItemChargeServiceImpl implements DictItemChargeService {
    @Autowired
    DictItemChargeMapper dictItemChargeMapper;

    @Override
    public void insert(DictEmployee dictEmployee, DictItemChargeVo dictItemChargeVo) {
        //价格添加规则：同一时间只能允许一个生效的价格，所以新增价格的同时需要先失效上个有效价格

        //作废当前生效的费用
        DictItemCharge dic= new DictItemCharge();

        dic.setItemid(dictItemChargeVo.getItemid());
        dic.setPriceChageRemak(dictItemChargeVo.getPriceChageRemak());
        dic.setInvalidDate(new Date());
        dic.setInvalidrId(dictEmployee.getEMPID());
        dic.setInvalidrName(dictEmployee.getEMPNAME());

        dictItemChargeMapper.updateForInvalidCharge(dic);

        //插入新价格
        //置空价格变动备注 这个备注是针对上个价格记录失效的备注
        DictItemCharge dictItemCharge = BeanUtil.copyBean(dictItemChargeVo,DictItemCharge.class);
        dictItemCharge.setId(WorkIdUtil.nextId());
        dictItemCharge.setTakeEffectDate(new Date());
        dictItemCharge.setCreaterId(dictEmployee.getEMPID());
        dictItemCharge.setCreaterName(dictEmployee.getEMPNAME());
        dictItemCharge.setPriceChageRemak(null);
        dictItemChargeMapper.insert(dictItemCharge);
    }
}
